package com.mano.mall.service.member.impl;

import com.mano.commons.web.constants.WebConstants;
import com.mano.mall.api.dto.member.input.UserLoginInpDTO;
import com.mano.mall.api.dto.member.output.UserOutputDTO;
import com.mano.mall.api.member.MemberService;
import com.mano.mall.common.base.BaseApiService;
import com.mano.mall.common.base.BaseResponse;
import com.mano.mall.common.constants.Constants;
import com.mano.mall.common.token.GenerateToken;
import com.mano.mall.common.utils.MD5Util;
import com.mano.mall.common.utils.MyBeanUtils;
import com.mano.mall.common.utils.TypeCastHelper;
import com.mano.mall.service.mapper.UserMapper;
import com.mano.mall.service.mapper.entity.UserDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @Author: zj
 * @Description:
 * @Date: Created in 13:23 2020/9/21
 * @Modified By:
 */
@RestController
public class MemberServiceImpl extends BaseApiService<UserOutputDTO> implements MemberService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private GenerateToken generateToken;

    @Override
    public BaseResponse<UserOutputDTO> existMobile(String mobile) {
        // 1.验证参数
        if (StringUtils.isEmpty(mobile)) {
            return setResultError("手机号码不能为空!");
        }
        UserDO userDO = userMapper.existMobile(mobile);
        if (userDO == null) {
            return setResultError(Constants.HTTP_RES_CODE_NOT_EXISTMOBILE_202, "用户不存在");
        }

        // 存在，那么就要对特殊敏感数据隐藏
        userDO.setPassword("**********");
        // 有些公司  邮箱的某些字段也要隐藏的

        UserOutputDTO userOutputDTO = MyBeanUtils.doToDto(userDO, UserOutputDTO.class);

        return setResultSuccess(userOutputDTO);
    }

    @Override
    public BaseResponse<UserOutputDTO> getInfo(String token) {
        // 1.参数验证
        if (StringUtils.isEmpty(token)) {
            return setResultError("token不能为空!");
        }
        // 2.使用token向redis中查询userId
        String redisValue = generateToken.getToken(token);
        if (StringUtils.isEmpty(redisValue)) {
            return setResultError("token已经失效或者不正确");
        }

        Long userId = TypeCastHelper.toLong(redisValue);
        // 3.根据userId查询用户信息
        UserDO userDO = userMapper.findByUserId(userId);
        if(null == userDO){
            return setResultError("用户信息不存在!");
        }

        // 4.将Do转换为Dto
        UserOutputDTO userOutputDTO = MyBeanUtils.doToDto(userDO, UserOutputDTO.class);

        return setResultSuccess(userOutputDTO);
    }

    /**
     * SSO认证系统登陆接口
     *
     * @param userLoginInpDTO
     * @return
     */

    @Override
    public BaseResponse<UserOutputDTO> ssoLogin(@RequestBody @Valid UserLoginInpDTO userLoginInpDTO) {

        // 1.验证参数
        String mobile = userLoginInpDTO.getMobile();

        String password = userLoginInpDTO.getPassword();

        String loginType = userLoginInpDTO.getLoginType();

        // 目的是限制范围
        if (!(loginType.equals(WebConstants.MEMBER_LOGIN_TYPE_ANDROID) || loginType.equals(WebConstants.MEMBER_LOGIN_TYPE_IOS)
                || loginType.equals(WebConstants.MEMBER_LOGIN_TYPE_PC))) {
            return setResultError("登陆类型出现错误!");
        }

        // 设备信息
        String deviceInfor = userLoginInpDTO.getDeviceInfor();

        // 2.对登陆密码实现加密
        String newPassWord = MD5Util.MD5(password);
        // 3.使用手机号码+密码查询数据库 ，判断用户是否存在
        UserDO userDo = userMapper.login(mobile, newPassWord);
        if (userDo == null) {
            return setResultError("用户名称或者密码错误!");
        }
        return setResultSuccess(MyBeanUtils.doToDto(userDo, UserOutputDTO.class));

    }
}
